package com.zhiche.lisa.lspm.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.carrier.CarrierInfo;
import com.zhiche.lisa.lspm.dao.model.carrier.bo.CarrierInfoLineBO;
import com.zhiche.lisa.lspm.dao.model.driver.Driver;
import com.zhiche.lisa.lspm.dao.model.driver.bo.DriverLineBO;
import com.zhiche.lisa.lspm.dao.model.fleet.Fleet;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerInfo;
import com.zhiche.lisa.lspm.dao.model.trailer.bo.TrailerInfoLineBO;
import com.zhiche.lisa.lspm.service.fleet.IFleetDriverService;
import com.zhiche.lisa.lspm.vo.driver.DriverBasicVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 	车队司机，牵引车，挂车登记控制器
 * @author fanguangji
 * @since 2018-11-07
 */
@RestController
@RequestMapping(value = "/fleetDriver")
@Api(value = "/driver", description = "车队司机，牵引车，挂车登记控制器", tags = {"车队司机，牵引车，挂车登记控制器"})
public class FleetDriverController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DriverController.class);
	
	@Autowired private IFleetDriverService fleetDriverService;
	
	@PostMapping(value = "/driverLine")
    @ApiOperation(value = "获取车队司机列表", notes = "获取车队司机列表", produces = MediaType.APPLICATION_JSON_VALUE,
            response = RestfulResponse.class)
    public RestfulResponse<Page<Map<String,Object>>> driverLine(@RequestBody Page<DriverLineBO> page) {
        LOGGER.info("FleetDriverController.driverLine（获取车队司机列表） params : {} ", page);

        RestfulResponse<Page<Map<String,Object>>> result = new RestfulResponse<>(CommonEnum.SUCCESS
                .getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Page<Map<String,Object>> pageVo = fleetDriverService.driverLine(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("FleetDriverController.driverLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetDriverController.driverLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }
	
	@PostMapping(value = "/updateDriverBasic")
    @ApiOperation(value = "车队修改新增司机基础信息", notes = "车队修改新增司机基础信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Driver> updateDriver(@RequestBody DriverBasicVO driver, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("DriverController.updateDriver" +
                "（车队修改新增司机基础信息） params : {} ", driver.toString());
        RestfulResponse<Driver> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Driver driverResult = fleetDriverService.updateDriver(driver, authorization);
            result.setData(driverResult);
        } catch (BaseException e) {
            LOGGER.error("DriverController.updateDriver ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("DriverController.updateDriver ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
	
	@GetMapping(value = "/getFleetInfo")
    @ApiOperation(value = "获取登陆者车队信息", notes = "获取登陆者车队信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<List<Fleet>> getFleetInfo(String fleetId, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetDriverController.updateDriver" +
                "（获取登陆者车队信息） params : {} ", fleetId);
        RestfulResponse<List<Fleet>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
        	List<Fleet> fleetResult = fleetDriverService.selectFleetInfo(fleetId, authorization);
            result.setData(fleetResult);
        } catch (BaseException e) {
            LOGGER.error("FleetDriverController.updateDriver ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetDriverController.updateDriver ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
	
	@PostMapping(value = "/carrierInfoLine")
    @ApiOperation(value = "获取车队牵引车基础信息列表", notes = "获取车队牵引车基础信息列表",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Page<Map<String,Object>>> carrierInfoLine(@RequestBody Page<CarrierInfoLineBO> page,
                                                                    @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetDriverController.carrierInfoLine（获取车队牵引车基础信息列表） params : {} ", page);

        RestfulResponse<Page<Map<String,Object>>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Page<Map<String,Object>> pageVo = fleetDriverService.carrierInfoLine(page, authorization);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("FleetDriverController.carrierInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetDriverController.carrierInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }
	
    @PostMapping(value = "/updateCarrier")
    @ApiOperation(value = "车队修改新增牵引车操作", notes = "车队修改新增牵引车操作",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<CarrierInfo> updateCarrier(@RequestBody CarrierInfo carrierInfo) {
        LOGGER.info("CarrierController.updateCarrier（车队修改新增牵引车操作） params : {} ", carrierInfo.toString());
        RestfulResponse<CarrierInfo> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            CarrierInfo carrierResult = fleetDriverService.updateCarrier(carrierInfo);
            result.setData(carrierResult);
        } catch (BaseException e) {
            LOGGER.error("CarrierController.updateCarrier ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CarrierController.updateCarrier ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }
    
    @PostMapping(value = "/trailerInfoLine")
    @ApiOperation(value = "车队获取挂车基础信息列表", notes = "车队获取挂车基础信息列表", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Page<Map<String,Object>>> trailerInfoLine(@RequestBody Page<TrailerInfoLineBO> page,@RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetDriverController.trailerInfoLine（车队获取挂车基础信息列表） params : {} ", page);

        RestfulResponse<Page<Map<String,Object>>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),CommonEnum.SUCCESS.getText(),null);

        try {
            Page<Map<String,Object>> pageVo = fleetDriverService.trailerInfoLine(page,authorization);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("FleetDriverController.trailerInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetDriverController.trailerInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }
    
    @PostMapping(value = "/updateTrailerBasic")
    @ApiOperation(value = "车队修改新增挂车", notes = "车队修改新增挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<TrailerInfo> updateTrailer(@RequestBody TrailerInfo trailerInfoParam) {
        LOGGER.info("FleetDriverController.updateTrailer（车队修改新增挂车） params : {} ", trailerInfoParam.toString());
        RestfulResponse<TrailerInfo> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            TrailerInfo lspInfo = fleetDriverService.updateTrailer(trailerInfoParam);
            result.setData(lspInfo);
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.updateTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.updateTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

}
